//
//  WebViewController.m
//  SSWiFi
//
//  Created by 酷安 @啥也不会啊啊啊啊啊啊  on 2024/1/24.
//

#import "WebViewController.h"
#import <WebKit/WebKit.h>

@interface WebViewController ()<WKNavigationDelegate>
@property (strong,nonatomic) WKWebView *webView;
@property (strong,nonatomic) UIProgressView *progressView;
@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSURL *url = [NSURL URLWithString:self.urlstr];
//    NSURLRequest *req = [NSURLRequest requestWithURL:url];
//    [self.webView loadRequest:req];
    [self refresh:self.urlstr];
//    [self.view insertSubview:self.emptyView aboveSubview:self.webView];
    if(self.title.length == 0){
        self.navigationItem.title = @"Web";
    }
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
    if(self.webvcType == WebVCTypeSpeed){
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithTitle:@"设置" target:self action:@selector(settingClick)];
    }
}

- (void)refresh:(NSString *)urlstr{
    NSURL *url = [NSURL URLWithString:urlstr];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:req];
}

// 实现 KVO 观察者方法以更新进度条
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        // 更新进度条
        CGFloat progress = self.webView.estimatedProgress;
        [self.progressView setProgress:progress animated:NO];

        // 进度达到 1.0 时隐藏进度条
        if (progress >= 1.0) {
            self.progressView.alpha = 0.0;
        } else {
            // 显示进度条
            self.progressView.alpha = 1.0;
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    NSLog(@"web dealloc");
}
- (WKWebView *)webView{
    if(!_webView){
        _webView = [[WKWebView alloc] init];
        [self.view addSubview:_webView];
        _webView.navigationDelegate = self;
//        _webView.UIDelegate = self;
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    return _webView;
}
- (UIProgressView *)progressView{
    if(!_progressView){
        _progressView = [[UIProgressView alloc]init];
        _progressView.tintColor = [UIColor greenColor];
        [_progressView setProgress:0];
        [self.view insertSubview:_progressView aboveSubview:self.webView];
        CGFloat h = 1.0;
        [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.height.equalTo(@(h));
            make.top.equalTo(self.navigationController.navigationBar.mas_bottom).offset(0);
        }];
        
    }
    return _progressView;
}


- (void)settingClick{
    MJWeakSelf;
    [Utils alertTextDig:@"修改网站地址" placehold:@"https://test.ustc.edu.cn/" text:self.urlstr keyboardType:UIKeyboardTypeASCIICapable enablesSubmitButtonAutomatically:NO block:^(NSString *value, QMUIDialogViewController *aDialogViewController) {
        value = value.qmui_trim;
        if(value.length == 0){
            value = @"https://test.ustc.edu.cn/";
        }
        if(![value hasPrefix:@"http"]){
            [QMUITips showError:@"请输入完整的网址(http开头)" inView:aDialogViewController.view];
            return;
        }
        else{
            [aDialogViewController hide];
            if([self.urlstr isEqual:value]){
                return;
            }else{
                [Utils saveSpeedUrl:value];
                self.urlstr = value;
                [weakSelf refresh:value];
            }
        }
    }];
}

@end
